﻿Public Class clsAccountGetAccessToken
    Dim web As WebBrowser
    Dim ParseHTML As New clsParseHTML
    Dim timCheck As Timer
    Dim is_run = False
    Dim Timeout As Integer = 30
    Dim SecondWait As Integer = 0
    Dim RenewAccessToken As Integer = 0
    Dim TimeRenewAccessToken = 3000
    Dim is_get_access_token As Boolean = False
    Public Sub New()
        timCheck = New Timer
        timCheck.Enabled = True
        timCheck.Interval = 1000
        AddHandler timCheck.Tick, AddressOf timCheck_Tick
        web = New WebBrowser
        web.ScriptErrorsSuppressed = True
        AddHandler web.DocumentCompleted, AddressOf web_DocumentCompleted
        AddHandler web.Navigating, AddressOf web_Navigating
    End Sub
    Private Sub TimeoutProcess()
        is_run = False
        getAccessToken()
        Debug.Print("Timeout Get access token")
    End Sub
    Private Sub timCheck_Tick()
        If (is_run = True) Then
            SecondWait = SecondWait + 1
            If SecondWait > Timeout Then
                is_run = False
                TimeoutProcess()
            End If
        End If
        RenewAccessToken = RenewAccessToken + 1
        If (RenewAccessToken > TimeRenewAccessToken) Then
            getAccessToken()
            RenewAccessToken = 0
        End If
    End Sub

    Public Sub getAccessToken()
        If (frmMain.Account.Login.infoLogin.FacebookID) Then
            is_run = True
            web.ScriptErrorsSuppressed = True
            web.Navigate("https://m.facebook.com/v2.1/dialog/oauth?response_type=token&client_id=145634995501895&redirect_uri=https%3A%2F%2Fdevelopers.facebook.com%2Ftools%2Fexplorer%2Fcallback&scope=user_about_me%2Cuser_actions.books%2Cuser_actions.music%2Cuser_actions.news%2Cuser_actions.video%2Cuser_activities%2Cuser_birthday%2Cuser_education_history%2Cuser_events%2Cuser_friends%2Cuser_games_activity%2Cuser_groups%2Cuser_hometown%2Cuser_interests%2Cuser_likes%2Cuser_location%2Cuser_photos%2Cuser_relationship_details%2Cuser_relationships%2Cuser_religion_politics%2Cuser_status%2Cuser_tagged_places%2Cuser_videos%2Cuser_website%2Cuser_work_history%2Cemail%2Cmanage_notifications%2Cmanage_pages%2Cpublish_actions%2Cread_friendlists%2Cread_insights%2Cread_mailbox%2Cread_page_mailboxes%2Cread_stream%2Crsvp_event", Nothing, Nothing, "User-Agent: mobile\r\n")
        End If
    End Sub

    Private Sub getAccessTokenSuccess()
        is_run = False
        Debug.Print(frmMain.Account.Login.infoLogin.AccessToken)
    End Sub

    Private Sub web_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs)
        If (web.Url IsNot Nothing And is_get_access_token = False) Then
            Dim pos_access_token As Integer = InStr(web.Url.ToString, "access_token=", CompareMethod.Text)
            If (pos_access_token > 0) Then
                e.Cancel = True
                Debug.Print(web.Url.ToString)
                web.DocumentText = "a"
                is_get_access_token = True
                frmMain.Account.Login.infoLogin.AccessToken = Mid(web.Url.ToString, pos_access_token + 13, InStr(web.Url.ToString, "&expires", CompareMethod.Text) - pos_access_token - 13)
                getAccessTokenSuccess()
            End If
        Else
            is_get_access_token = False
        End If
        Application.DoEvents()
    End Sub
    Private Sub web_DocumentCompleted()
        If (frmMain.is_debug = True) Then
            frmDebug.web_debug_get_access_token.Document.Write("<html><head><title></title><body>" & web.DocumentText & "</body></html>")
            frmDebug.txt_debug_get_access_token.Text = web.DocumentText
        End If
        Debug.Print(web.Url.ToString)
        ParseHTML.SetSubmitById(web, "platformDialogForm")
        'Dim pos_access_token As Integer = InStr(web.Url.ToString, "callback", CompareMethod.Text)
        'If (pos_access_token > 0) Then
        'web.DocumentText = Replace("<script>", "[script]", web.DocumentText)
        'frmMain.Account.Login.infoLogin.AccessToken = Mid(web.Url.ToString, pos_access_token + 13, InStr(web.Url.ToString, "&expires", CompareMethod.Text) - pos_access_token - 13)
        'getAccessTokenSuccess()
        ' web.Stop()
        'Exit Sub
        'End If

    End Sub

End Class
